package com.ican.action.product;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ican.common.BusinessException;
import com.ican.common.hibernate.Page;
import com.ican.domain.product.Option;
import com.ican.domain.product.OptionValue;
import com.ican.oc.domain.OcOptionDescription;
import com.ican.oc.domain.OcOptionValueDescription;
import com.ican.oc.service.OcOptionAndValueService;
import com.ican.service.product.OptionAndValueService;
import com.ican.service.product.ProductService;
import com.ican.util.ContextUtils;
import com.opensymphony.xwork2.ActionSupport;

public class SynAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = LoggerFactory.getLogger(SynAction.class);
    
    private OptionAndValueService optionAndValueService;
    private ProductService productService;
    
    private OcOptionAndValueService ocOptionAndValueService;
    
    private String ajaxPara;
    
    //第一步，配置属性
    private Page<Option> optionPage = new Page<Option>();
    public String toConfigOpton() {
        return "optionprocedure";
    }
    //提取没有配置属性名称映射的属性
    public String callOptionProcedure() {
        try{
            optionAndValueService.createOption();
            ContextUtils.successMsg2Session(null);
            return toConfigOptionName();
        }catch(BusinessException e){
            ContextUtils.errorMsg2Session(e.getMessage());
            logger.error(e.getMessage(),e);
        }catch(Exception e){
            ContextUtils.errorMsg2Session(null);
            logger.error(e.getMessage(),e);
        }
        return "optionprocedure";
    }
    //更新属性名称
    public String toConfigOptionName() {
        optionPage = optionAndValueService.findAllOption2Syn(optionPage);
        return "optionname";
    }
    public void modifyOptionName(){
        try{
            String[] params = ajaxPara.split("#");
            if(params.length != 4 || params[3].trim().length() < 1){
                ContextUtils.write2Response("参数为空！");
            }else{
                Integer id = Integer.valueOf(params[0]);
                String name = params[1];
                Integer langId = Integer.valueOf(params[2]);
                String nameNew = params[3].trim();
                if(!nameNew.equals(name)){
                    //更新属性名称
                    productService.updateAttrOptionName(nameNew, name, langId);
                    optionAndValueService.deleteOrUpdateOptionName(id, nameNew, langId);
                    ContextUtils.write2Response("OK");
                }
            }
        }catch(BusinessException e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }catch(Exception e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }
    }
    //配置属性名称映射
    public String toConfigOptionCode() {
        optionPage = optionAndValueService.findAllOption2Syn(optionPage);
        return "optioncode";
    }
    public void modifyOptionCode(){
        try{
            String[] params = ajaxPara.split("#");
            if(params.length != 4 || params[3].trim().length() < 1){
                ContextUtils.write2Response("参数为空！");
            }else{
                Integer id = Integer.valueOf(params[0]);
                String name = params[1];
                Integer langId = Integer.valueOf(params[2]);
                String nameCode = params[3].trim();
                if(nameCode.equals("-1")){
                    //删除属性
                    productService.deleteductAttr(name, langId);
                    optionAndValueService.deleteOption(id);
                    ContextUtils.write2Response("OK");
                }else{
                    //更新属性
                    OcOptionDescription opDes = ocOptionAndValueService.addOptionAndDesc(nameCode, name, langId);
                    productService.updateAttrOptionCode(name, langId, nameCode);
                    optionAndValueService.updateOptionCode(id, nameCode, opDes.getPk().getOptionId());
                    ContextUtils.write2Response("OK");
                }
            }
        }catch(BusinessException e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }catch(Exception e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }
    }
    
    //第二步，配置属性值
    private Page<OptionValue> optionValuePage = new Page<OptionValue>();
    public String toConfigOptonValue() {
        return "optvalprocedure";
    }
    //提取没有配置属性值名称映射的属性值
    public String callOptionValueProcedure() {
        try{
            optionAndValueService.createOptionValue();
            ContextUtils.successMsg2Session(null);
            return toModifyOptionValue();
        }catch(BusinessException e){
            ContextUtils.errorMsg2Session(e.getMessage());
            logger.error(e.getMessage(),e);
        }catch(Exception e){
            ContextUtils.errorMsg2Session(null);
            logger.error(e.getMessage(),e);
        }
        return "optvalprocedure";
    }
    //更新属性值名称
    public String toModifyOptionValue() {
        optionValuePage = optionAndValueService.findAllOptionValue2Syn(optionValuePage);
        return "optvalname";
    }
    public void modifyOptionValue(){
        try{
//            id + "#" + nameCode + "#" + ocOptionId + "#" + optionValue + "#" + langId + "#" +$(optionValue).val();
            String[] params = ajaxPara.split("#");
            if(params.length != 6 || params[5].trim().length() < 1){
                ContextUtils.write2Response("参数为空！");
            }else{
                Integer id = Integer.valueOf(params[0]);
                String nameCode = params[1];
//                Integer ocOptionId = Integer.valueOf(params[2]);
                String optionValue = params[3];
                Integer langId = Integer.valueOf(params[4]);
                String optionValueNew = params[5].trim();
                //更新属性值名称
                productService.updateAttrOptionValueName(nameCode, optionValue, langId, optionValueNew);
                optionAndValueService.deleteOrUpdateOptionValueName(id, nameCode, optionValueNew, langId);
                ContextUtils.write2Response("OK");
            }
        }catch(BusinessException e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }catch(Exception e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }
    }

    //配置属性值映射
    public String toModifyOptionValueCode() {
        optionValuePage = optionAndValueService.findAllOptionValue2Syn(optionValuePage);
        return "optvalcode";
    }
    public void modifyOptionValueCode(){
        try{
//            id + "#" + nameCode + "#" + ocOptionId + "#" + optionValue + "#" + langId + "#" +$(valueCode).val();
            String[] params = ajaxPara.split("#");
            if(params.length != 6 || params[5].trim().length() < 1){
                ContextUtils.write2Response("参数为空！");
            }else{
                Integer id = Integer.valueOf(params[0]);
                String nameCode = params[1];
                Integer ocOptionId = Integer.valueOf(params[2]);
                String optionValue = params[3];
                Integer langId = Integer.valueOf(params[4]);
                String valueCode = params[5].trim();
                if(valueCode.equals("-1")){
                    //删除属性值
                    productService.deleteductAttr(nameCode, optionValue, langId);
                    optionAndValueService.deleteOptionValue(id);
                    ContextUtils.write2Response("OK");
                }else{
                    //更新属性值
                    OcOptionValueDescription opValDes = ocOptionAndValueService.addOptionValueAndDesc(ocOptionId, valueCode, optionValue, langId);
                    productService.updateAttrOptionValueCode(nameCode, optionValue, valueCode, langId);
                    optionAndValueService.updateOptionValueCode(id, valueCode, opValDes.getPk().getOptionValueId());
                    ContextUtils.write2Response("OK");
                }
            }
        }catch(BusinessException e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }catch(Exception e){
            logger.error(e.getMessage(),e);
            ContextUtils.write2Response(e.getMessage());
        }
    }

    @Resource
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }
    @Resource
    public void setOcOptionAndValueService(OcOptionAndValueService ocOptionAndValueService) {
        this.ocOptionAndValueService = ocOptionAndValueService;
    }
    @Resource
    public void setOptionAndValueService(OptionAndValueService optionAndValueService) {
        this.optionAndValueService = optionAndValueService;
    }
    public String getAjaxPara() {
        return ajaxPara;
    }
    public void setAjaxPara(String ajaxPara) {
        this.ajaxPara = ajaxPara;
    }
    public Page<Option> getOptionPage() {
        return optionPage;
    }
    public void setOptionPage(Page<Option> optionPage) {
        this.optionPage = optionPage;
    }
    public Page<OptionValue> getOptionValuePage() {
        return optionValuePage;
    }
    public void setOptionValuePage(Page<OptionValue> optionValuePage) {
        this.optionValuePage = optionValuePage;
    }
}
